﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.RoomAvaAndBooking
{
    public partial class ResRecordFrm : Form
    {
        public ArrayList resList;
        public int selectedResNo;
        public string selectedCusIC;
        public int selectedRoomID;
        private string resStatus;

        public ResRecordFrm()
        {
            InitializeComponent();
        }

        #region form load
        private void ResRecord_Load(object sender, EventArgs e)
        {
            if (resList.Count != 0)
            {
                dgvReservations.DataSource = resList;
                SetDataGridViewHeader();
            }
            else
            {
                dgvReservations.DataSource = resList;
            }
        }
        #endregion

        #region manage datagridview
        private void SetDataGridViewHeader()
        {
            dgvReservations.Columns[0].HeaderText = "Reservation No.";
            dgvReservations.Columns[1].HeaderText = "Customer IC";
            dgvReservations.Columns[2].HeaderText = "Room ID";
            dgvReservations.Columns[3].HeaderText = "No. of Adult";
            dgvReservations.Columns[4].HeaderText = "No. of Children";
            dgvReservations.Columns[5].HeaderText = "Check In Date Time";
            dgvReservations.Columns[6].HeaderText = "Check Out Date Time";
            dgvReservations.Columns[7].HeaderText = "Reservation Status";
            dgvReservations.Columns[8].HeaderText = "Remark";
            dgvReservations.Columns[9].HeaderText = "Record Date Time";
        }

        private void dgvReservations_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                DataGridViewTextBoxCell cellResNo = (DataGridViewTextBoxCell)
                    dgvReservations.Rows[e.RowIndex].Cells[0];
                DataGridViewTextBoxCell cellCusIC = (DataGridViewTextBoxCell)
                    dgvReservations.Rows[e.RowIndex].Cells[1];
                DataGridViewTextBoxCell cellRoomID = (DataGridViewTextBoxCell)
                    dgvReservations.Rows[e.RowIndex].Cells[2];
                DataGridViewTextBoxCell cellResStatus = (DataGridViewTextBoxCell)
                    dgvReservations.Rows[e.RowIndex].Cells[7];

                selectedResNo = Convert.ToInt32(cellResNo.Value);
                tbxResNo.Text = selectedResNo.ToString();
                selectedCusIC = Convert.ToString(cellCusIC.Value);
                //tbxCusIC.Text = selectedCusIC;
                selectedRoomID = Convert.ToInt16(cellRoomID.Value);
                tbxRoomID.Text = selectedRoomID.ToString();
                resStatus = Convert.ToString(cellResStatus.Value);
                tbxResStatus.Text = resStatus.ToString();
            }
            catch
            {
                
            }
        }
        #endregion 

        #region check out button
        private void btnCheckOut_Click(object sender, EventArgs e)
        {
            if (tbxResNo.TextLength == 0)
            {
                MessageBox.Show("Please select a record to continue!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tbxResStatus.Text.Equals("Completed"))
            {
                MessageBox.Show("Please select a not completed record to continue!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            CheckOutFrm frm = new CheckOutFrm();
            frm.cus = RoomAvaAndBookingDBManager.GetCusByIC(selectedCusIC);
            frm.room = RoomAvaAndBookingDBManager.GetRoomByID(selectedRoomID.ToString());
            frm.res = RoomAvaAndBookingDBManager.GetReservationByNum(selectedResNo.ToString());
            //this.Close();
            frm.ShowDialog();
        }
        #endregion

        #region radio button filters
        private void rbnCompleted_CheckedChanged(object sender, EventArgs e)
        {
            if (rbnCompleted.Checked == true)
            {
                ArrayList newList = RoomAvaAndBookingDBManager.GetReservationByStatus("Completed");
                dgvReservations.DataSource = newList;
                //SetDataGridViewHeader();
            }

        }

        private void rbnStaying_CheckedChanged(object sender, EventArgs e)
        {
            if (rbnStaying.Checked == true)
            {
                ArrayList newList = RoomAvaAndBookingDBManager.GetReservationByStatus("Staying");
                dgvReservations.DataSource = newList;
                //SetDataGridViewHeader();
            }

        }

        private void rbnReserving_CheckedChanged(object sender, EventArgs e)
        {
            if (rbnReserving.Checked == true)
            {
                ArrayList newList = RoomAvaAndBookingDBManager.GetReservationByStatus("Reserving");
                dgvReservations.DataSource = newList;
                //SetDataGridViewHeader();
            }
        }

        private void rbnAll_CheckedChanged(object sender, EventArgs e)
        {
            if (rbnAll.Checked == true)
            {
                ArrayList newList = RoomAvaAndBookingDBManager.GetAllReservations();
                dgvReservations.DataSource = newList;
                //SetDataGridViewHeader();
            }
        }

        #endregion

        #region check in button
        private void btnCheckIn_Click(object sender, EventArgs e)
        {
            if (tbxResNo.TextLength == 0)
            {
                MessageBox.Show("Please select a record to continue!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            CheckInFrm frm = new CheckInFrm();
            frm.cusIC = selectedCusIC;
            frm.roomIDFromResRecordFrm = selectedRoomID.ToString();
            frm.room = RoomAvaAndBookingDBManager.GetRoomByID(selectedRoomID.ToString());
            frm.res = RoomAvaAndBookingDBManager.GetReservationByNum(selectedResNo.ToString());
            frm.isFromRes = true;
            frm.checkICStatus = true;
            //this.Close();
            frm.ShowDialog();
        }
        #endregion

        #region search
        private void btnSearchReserving_Click(object sender, EventArgs e)
        {
            string roomID = tbxRoomID.Text;
            ArrayList searchList = RoomAvaAndBookingDBManager.GetReservationByRmIDStatus(roomID, "Reserving");
            dgvReservations.DataSource = searchList;
        }

        private void btnSearchStaying_Click(object sender, EventArgs e)
        {
            string roomID = tbxRoomID.Text;
            ArrayList searchList = RoomAvaAndBookingDBManager.GetReservationByRmIDStatus(roomID, "Staying");
            dgvReservations.DataSource = searchList;
        }
        #endregion

    }
}
